package com.kaisademo.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaisademo.zhxy.mapper.StudentMapper;
import com.kaisademo.zhxy.pojo.Admin;
import com.kaisademo.zhxy.pojo.LoginForm;
import com.kaisademo.zhxy.pojo.Student;
import com.kaisademo.zhxy.service.StudentService;
import com.kaisademo.zhxy.util.MD5;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Override
    public Student login(LoginForm loginForm) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getName,loginForm.getUsername());
        queryWrapper.eq(Student::getPassword, MD5.encrypt(loginForm.getPassword()));
        Student student = baseMapper.selectOne(queryWrapper);
        return student;
    }

    @Override
    public Student getAdminById(Long userId) {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Student::getId,userId);
        Student student = baseMapper.selectOne(lambdaQueryWrapper);
        return student;
    }

    @Override
    public IPage<Student> getStudentByOpr(Page<Student> studentPage, Student student) {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(student.getClazzName() != null){
            lambdaQueryWrapper.eq(Student::getClazzName,student.getClazzName());
        }
        if(student.getName() != null){
            lambdaQueryWrapper.like(Student::getName,student.getName());
        }
        lambdaQueryWrapper.orderByDesc(Student::getId);
        Page<Student> selectPage = baseMapper.selectPage(studentPage, lambdaQueryWrapper);
        return selectPage;
    }
}
